package com.ls.file;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.apache.log4j.Logger;

public class RandomFile {
	Logger logger = Logger.getLogger(RandomFile.class);
	public void randomRead(String fileName,int position) throws IOException{
		String path= System.getProperty("user.dir")+File.separator+"resource"+File.separator+fileName;
		RandomAccessFile file = new RandomAccessFile(path,"r");
		logger.info("init pointor is:"+file.getFilePointer());
		file.seek(position);
		byte[] buf = new byte[1024];
		int readCount = 0;
		while((readCount = file.read(buf))>-1){
			logger.info(new String(buf,0,readCount));
		}
		file.close();
	}
	
	public void randWrite(String fileName,String content)throws IOException{
		String path= System.getProperty("user.dir")+File.separator+"resource"+File.separator+fileName;
		RandomAccessFile file = new RandomAccessFile(path,"rw");
		file.seek(file.length());
		file.write(content.getBytes());
		file.close();
	}
	
	public static void main(String[] args) throws IOException {
//		new RandomFile().randomRead("randomFile.txt", 10);
		new RandomFile().randWrite("randomFile.txt", "append to tail");
	}
	
}
